package com.lagou.controller;

import com.lagou.entity.CustomUser;
import com.lagou.response.ResponseResult;
import com.lagou.service.MeetService;
import com.lagou.utils.helper.RedisLockHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 会议信息
 */
@RestController(value = "MeetController")
@RequestMapping(value = "/custom/meet")
@Slf4j
public class MeetController {

    @Autowired
    private MeetService meetService;

    @Autowired
    private RedisLockHelper redisLockHelper;

    @Autowired
    @Qualifier(value = "redis")
    private RedisTemplate<String, String> redisTemplate;


    /**
     * map 用map接收参数
     * @param param param
     * @return
     */
    @RequestMapping(value = "/redis", method = RequestMethod.GET)
    public ResponseResult<String> redis(@RequestParam Map<String, Object> param) {
        String permission = redisTemplate.opsForValue().get("permission");
        if (StringUtils.isBlank(permission)) {
            redisTemplate.opsForValue().set("permission", "result", 5L, TimeUnit.SECONDS);
            return ResponseResult.success(permission);
        } else {
            redisTemplate.delete("permission");
            return ResponseResult.success(permission);
        }
    }

    /**
     * map 用map接收参数
     * @param param param
     * @return
     */
    @RequestMapping(value = "/map", method = RequestMethod.GET)
    public ResponseResult<String> map(@RequestParam Map<String, Object> param) {
        return ResponseResult.success();
    }

    /**
     * 分布式锁测试
     *
     * @return 是否拿到锁
     */
    @RequestMapping(value = "/getLock", method = RequestMethod.GET)
    public ResponseResult<String> getLock() {
        long time = System.currentTimeMillis();
        try {
            boolean lock = redisLockHelper.lock("lock", String.valueOf(time));
            if (lock) {
                return ResponseResult.success("成功拿到锁");
            } else {
                return ResponseResult.success("未拿到锁");
            }
        } finally {
            redisLockHelper.unlock("lock");
        }
    }

    /**
     * 分页查询
     *
     * @param pageCurr 当前页
     * @param pageSize 当前页大小
     * @return ResponseResult.success(customUsers);
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResponseResult<List<CustomUser>> page(@RequestParam(value = "pageCurr") int pageCurr,
                                                 @RequestParam(value = "pageSize") int pageSize) {
        pageCurr = pageCurr < 0 ? 1 : pageCurr;
        pageSize = pageSize < 0 ? 5 : pageSize;
        List<CustomUser> customUsers = meetService.queryByPage(pageCurr, pageSize);
        return ResponseResult.success(customUsers);
    }

    /**
     * post
     *
     * @return ResponseResult
     */
    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public ResponseResult<CustomUser> post() {
        return ResponseResult.success(null);
    }

    /**
     * test
     *
     * @param record record
     * @return ResponseResult
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseResult<CustomUser> test(CustomUser record) {
        CustomUser customUser = meetService.queryUser(record);
        return ResponseResult.success(customUser);
    }
}
